import 'package:flutter/material.dart';
import '../../share/my_color.dart';
import '../../share/size_fill.dart';

typedef OnTopFutureBack = Future<bool> Function(bool);
typedef OnTopBack = Function(bool);

class ShowAttentionButtonWidget extends StatefulWidget {
  bool selected;
  final String normalText;
  final String selectText;
  final Color normalTextColor;
  final Color selectTextColor;
  final Color normalBgColor;
  final Color selectBgColor;

  final OnTopBack? onTop;
  double? radius;
  double? minWidth;
  double? height;
  double? font;
  final OnTopFutureBack? future;

  final Color norBorderSideColor;
  final double norBorderSideWidth;

  final Color selectBorderSideColor;
  final double selectBorderSideWidth;
  final EdgeInsetsGeometry padding;


  ShowAttentionButtonWidget({
    this.selected = false,
    this.onTop,
    this.font, //12
    this.height, //24
    this.minWidth, //52
    this.radius, //12
    this.normalText = "关注",
    this.normalBgColor = MyColors.color_255_255_255,
    this.norBorderSideWidth = 1,
    this.norBorderSideColor = MyColors.color_255_75_58,
    this.normalTextColor = MyColors.color_255_75_58,
    this.selectText = "已关注",
    this.selectBorderSideWidth = 1,
    this.selectBgColor = MyColors.color_245_245_245,
    this.selectTextColor = MyColors.color_170_170_170,
    this.selectBorderSideColor = MyColors.color_245_245_245,
    this.future,
    this.padding =const EdgeInsets.symmetric(horizontal: 5),
  });

  @override
  _ShowAttentionButtonWidgetState createState() => _ShowAttentionButtonWidgetState();
}

class _ShowAttentionButtonWidgetState extends State<ShowAttentionButtonWidget> {
  void onTopDid() {
    if (widget.onTop != null) {
      widget.onTop!(widget.selected);
    }
    if (widget.future != null) {
      widget.future!(widget.selected).then((value) {

        print("这里是回到$value");
        setState(() {
          widget.selected = value;
        });
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    if (widget.minWidth == null) {
      widget.minWidth = SizeFill.setRx(52);
    }
    if (widget.height == null) {
      widget.height = SizeFill.setRx(24);
    }
    if (widget.font == null) {
      widget.font = SizeFill.setRx(12);
    }
    if (widget.radius == null) {
      widget.radius = SizeFill.setRx(12);
    }
    //字体
    String text = widget.selected ? widget.selectText : widget.normalText;
    Color textColor = widget.selected ? widget.selectTextColor : widget.normalTextColor;
    Color bgColor = widget.selected ? widget.selectBgColor : widget.normalBgColor;
    Color borderSideColor = widget.selected ? widget.selectBorderSideColor : widget.norBorderSideColor;
    double borderSideWidth = widget.selected ? widget.selectBorderSideWidth : widget.norBorderSideWidth;

    return ButtonTheme(
      minWidth: widget.minWidth!,
      height: widget.height!,
      materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
      padding: widget.padding,
      child: Container(
        decoration: BoxDecoration(
          color: bgColor,
          borderRadius: BorderRadius.circular(widget.radius!),
        ),
        child: OutlineButton(
          onPressed: () {
            onTopDid();
          },
          borderSide: BorderSide(color: borderSideColor, width: borderSideWidth),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(
              widget.radius!,
            ),
          ),
          child: Text(
            text,
            style: TextStyle(color: textColor, fontWeight: FontWeight.w500),
          ),
        ),
      ),
    );
  }
}
